
package servlets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.paypal.svcs.services.AdaptivePaymentsService;
import com.paypal.svcs.types.ap.PayRequest;
import com.paypal.svcs.types.ap.PayResponse;
import com.paypal.svcs.types.ap.Receiver;
import com.paypal.svcs.types.ap.ReceiverList;
import com.paypal.svcs.types.common.RequestEnvelope;



public class StartPaymentServlet extends HttpServlet {
	
	private static final long serialVersionUID = -7621607903510563985L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		String emailOwner, cost, idUser, idAdvertisement, returnUrl;
		emailOwner = request.getParameter("emailOwner");
		cost = request.getParameter("cost");
		idUser = request.getParameter("idUser");
		idAdvertisement = request.getParameter("idAdvertisement");
		returnUrl = request.getParameter("returnUrl");
		System.out.println(returnUrl);
		System.out.flush();
		
		PayRequest payRequest = new PayRequest();
		  
		List<Receiver> receivers = new ArrayList<Receiver>();
		Receiver receiver = new Receiver();
		receiver.setAmount(Double.parseDouble(cost));
		
		//Payment receiver!!!!
		receiver.setEmail(emailOwner);
		receivers.add(receiver);
		ReceiverList receiverList = new ReceiverList(receivers);
	
		payRequest.setReceiverList(receiverList);
	
		RequestEnvelope requestEnvelope = new RequestEnvelope("en_US");
		payRequest.setRequestEnvelope(requestEnvelope); 
		payRequest.setActionType("PAY");
		payRequest.setCancelUrl("http://localhost:8080/ISellToYouRESTWebService/EndPayPalPayment?success=false&idUser=" + idUser + "&returnUrl="+ returnUrl);
		payRequest.setReturnUrl("http://localhost:8080/ISellToYouRESTWebService/EndPayPalPayment?success=true&idUser=" + idUser + "&idAdvertisement=" + idAdvertisement+ "&emailOwner="+ emailOwner + "&returnUrl="+ returnUrl);
		payRequest.setCurrencyCode("USD");
		payRequest.setIpnNotificationUrl("http://replaceIpnUrl.com");
	
		Map<String, String> sdkConfig = new HashMap<String, String>();
		sdkConfig.put("mode", "sandbox");
		sdkConfig.put("acct1.UserName", "servicetechbuyer_api1.gmail.com");
		sdkConfig.put("acct1.Password", "1399031982");
		sdkConfig.put("acct1.Signature","ADNLye8U.4IZG1KfNOJxk73CV1QUAIlZOIFsXBnpdpT0e1QpJwed2D3e");
		sdkConfig.put("acct1.AppId","APP-80W284485P519543T");
	
		AdaptivePaymentsService adaptivePaymentsService = new AdaptivePaymentsService(sdkConfig);
		try{
			PayResponse payResponse = adaptivePaymentsService.pay(payRequest);
			if(!payResponse.getPaymentExecStatus().equals("CREATED"))
				throw new Exception();
			
			request.getSession().setAttribute("payRkey", payResponse.getPayKey());
			response.sendRedirect("https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=" + payResponse.getPayKey());
		}catch(Exception e){
			System.err.println(e);
			e.printStackTrace();
			try {
				response.sendRedirect("http://localhost:8080/ISellToYouRESTWebService/EndPayPalPayment?success=false&idUser=" + idUser + "&returnUrl="+ returnUrl);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
